// ================================================== //
// =============== PROJET BEBOP 1.0 ================= //
// ================================================== //
// ================ CALENDRIER.ASM ================== //

// Code source en assembleur visa pour le calendrier
// Ocan Sankur

// Jour/Mois/An Sec/Min/Heure Jour%7
// Americains: attention a la crise de l'annee 3000
//GLOBAL VARIABLE DECLARATIONS (ADDRESSES)
define fevrier 7
define year 5
define month 4
define day 3
define hour 2
define min 1
define sec 0
define dayname 6
define days_in_current_month 8
define tmp 9
//pour x constante, minus x donne -x dans R1
macro minus x
	load $x, R0
	not R0
	load #1, R1
	add
end
//increment var by 1. if it equals modulo goto carry_label, sinon to return_label
macro increment var, modulo,  return_label, carry_label
	load $var,R0
	load #1,R1
	add
	store R1,$var
	load $modulo,R0
	cmp
	jmn $carry_label
	jmp $return_label
end
start

//init de la date 
//load #7,R0
//store R0,year
//load #11,R0
//store R0,month
//load #31,R0
//store R0,day
//load #23,R0
//store R0,hour
//load #59,R0
//store R0,min
//load #59,R0
//store R0, sec
//load #6,R0
//store R0,dayname

//calcul du fevrier:
CALC_FEVRIER: load year,R0
load #3,R1
and
//si c'est divisible par 4
jmz @29
jmn @28
29: load #29,R0
jmp @FEB_WRITE
28: load #28,R0
FEB_WRITE: store R0,fevrier

//MAIN PROGRAM
UPDATE_SEC:
	//update dev out SEC
	load #0, R0
	load sec, R1
	//device adress:
	store R0, 255
	//dev data
	store R1, 255

	//update dev out MIN
	load #1, R0
	load min, R1
	//device adress:
	store R0, 255
	//dev data
	store R1, 255

	//update dev out HOUR
	load #2, R0
	load hour, R1
	//device adress:
	store R0, 255
	//dev data
	store R1, 255

	//update dev out DAY
	load #4, R0
	load day, R1
	//device adress:
	store R0, 255
	//dev data
	store R1, 255

	//update dev out MONTH
	load month, R1
	//device adress:
	load #5, R0
	store R0, 255
	//dev data
	store R1, 255

	//update dev out YEAR
	load #6, R0
	load year, R1
	//device adress:
	store R0, 255
	//dev data
	store R1, 255
sleep
COUNT_SEC: increment sec,#60,@UPDATE_SEC,@COUNT_MIN

COUNT_MIN: load #0,R0
store R0,sec
increment min,#60,@UPDATE_SEC,@COUNT_HOUR

COUNT_HOUR: load #0,R0
store R0,min
increment hour,#24,@UPDATE_SEC,@COUNT_DAY

COUNT_DAY: load #0,R0
store R0,hour
//calculate days_in_current_month
load month, R0

//is it Fevrier
load #1, R1
cmp
jmn @FEVRIER

//Sinon month <= 6 ==> (a paire <==> 31) et month > 6 ==> (a paire <==> 30
minus #6
load month, R0
add
//si ca fait 0, c'est que month == 6
load #0, R0
cmp
jmn @L6
//sinon on va voir le bit 2^7. Si c'est 1 c'est que le resultat etait negatif <==> month < 6
minus #6
load month, R0
add
load #128, R0
and
cmp
jmn @L6
jmp @G6
DONE: increment day, days_in_current_month, @UPDATE_SEC, @COUNT_MONTH

COUNT_MONTH: load #0,R0
store R0, day
increment month, #12, @UPDATE_SEC, @COUNT_YEAR

COUNT_YEAR: load #0,R0
store R0, month
increment year, #100, @CALC_FEVRIER, @RINIT
RINIT: load #0,R0
store R0, year
jmp @UPDATE_SEC

//MORCEAUX DE CODE (FONCTIONS AVEC GOTO) POUR LE CALCUL DE NB DE JOUR DU MOIS COURANT:

FEVRIER: load fevrier, R0
store R0, days_in_current_month
jmp @DONE

//Cas mois > 6. 
G6: 
load month, R0
load #1, R1
and
jmz @F30
F31: load #31, R0
jmp @W
F30: load #30, R0
W: store R0, days_in_current_month
jmp @DONE

// <= 6. R0 contient toujours le numero du mois
L6: 
load month, R0
load #1, R1
and
jmn @F30
jmp @F31
